var layer;
var form;
var table;
var tree;
var columnObjName = "column_role";
var columnArray = ['rname','state','type','createDate','updateDate','remarks'];
var interUrl = {
		select: $projectPath+'/sys/role/select',
		save: $projectPath+'/sys/role/save',
		del: $projectPath+'/sys/role/delete',
		delBatch: $projectPath+'/sys/role/deleteBatch',
		selectRoleMenuTree: $projectPath+"/sys/role/selectRoleMenuTree",
		saveRoleMenu: $projectPath+"/sys/role/saveRoleMenu",
		updateState: $projectPath+"/sys/role/updateState",
}

/**	
 * layui 组件引入
 * @returns
 */
layui.use([ 'layer', 'form', 'table', 'tree'], function() {
	layer = layui.layer;
	form = layui.form;
	table = layui.table;
	tree = layui.tree;
	
	// 加载角色
    getDictLabel("use_state", "#state", "state", true);
    getDictLabel("use_state", "#search_state", "state", false, 3);
    
    // 执行一个 table 实例
	table.render({
		elem : '#data_table', 
		id : 'data_table', 
		url : interUrl.select, 
		title : '角色', 
		height:'full-120', 
		page : true,
		toolbar : "#toolberLeft", 
		cols : [ [
			{type : 'checkbox', fixed : 'left'},  
			{field : 'rname', title : '角色名称', width: '150', align : 'center', hide: setColumnHide("rname")}, 
			{field : 'state', title : '使用状态', width: '150', align : 'center', hide: setColumnHide("state"), toolbar : '#switchRow'},  
			{field : 'type', title : '类型', width: '150', sort : true, align : 'center', hide: setColumnHide("type")},  
			{field : 'createDate', align : 'center', width: '200', title : '创建时间', hide: setColumnHide("createDate")}, 
			{field : 'updateDate', align : 'center', width: '200', title : '更新时间', hide: setColumnHide("updateDate")}, 
			{field : 'remarks', align : 'center', width: '200', title : '备注', hide: setColumnHide("remarks")}, 
			{fixed : isRight(), align : 'center', width: '250', title : '操作', toolbar : '#toolRow'} 
		] ]
	});
    
	// 头部左侧事件
	table.on('toolbar(data_table)',  function(obj) {
		if (obj.event === 'search') {
			openWindowT('search_form', "条件搜索", '100%', '150px', false, 0, false, false, null, 0.2, true)
		}else if(obj.event === 'add'){
			openWindow('data_form', "角色新增", '500px', '500px', false, 0, false, false, null, 0.2, false)
		}else if(obj.event === 'delete'){
            deleteBatch(table, 'data_table', "rid", interUrl.delBatch)
		}
	})
	
	// 行监听事件
	table.on('tool(data_table)',  function(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			deleteRow(table, 'data_table', data, interUrl.del);
		}else if(obj.event === 'edit') {
			openWindow('data_form', "角色编辑", '500px', '500px', false, 0, false, false, data, 0.2, false)
		}else if(obj.event === 'detail') {
			openWindow('data_form', "角色查看", '500px', '500px', false, 0, false, false, data, 0.2, false)
		}else if(obj.event === 'power') {
			openWindowRB('role_menu_form', "授权菜单", '500px', '100%', false, 0, false, false, data, 0.2, false, function(res){
				getAjax(interUrl.selectRoleMenuTree, {rid: res.rid}, 'post', function (res) {
			        if (res.success) {
			        	var list = [{"id": 0, "title": "全选", "spread": true, children: res.data}];
			        	tree.render({
			        		elem: '#menu_tree'
			        		,data: list
							,showCheckbox: true  //是否显示复选框
							,id: 'menu'
			        	});
			        }else{
			        	showTips(res.msg);
			        }
			    })
			})
		} 
	});
	
	// 查询
	form.on('submit(submitSearch)',  function(data) {
		reloadTable(table, 'data_table', data.field);
		return false;
	})
	
	// 提交
	form.on('submit(submitForm)',  function(data) {
		saveData(table, 'data_table', data.field, interUrl.save, '#data_form');
		return false;
	})
    
	// 提交角色菜单
	form.on('submit(roleMenuForm)', function(data) {
		var rid = data.field.rid;
		var checkData = tree.getChecked('menu');
	    var mids = [];
	    getTreeValue(checkData, mids);
	    var text = sbumitLoading('#role_menu_form .lay-submit-btn');
	    getAjax(interUrl.saveRoleMenu, {mids: mids.join(","), rid: rid}, 'post', function (res) {
	        if (res.success) {
	            layer.closeAll();
	        }
	        showTips(res.msg);
	        closeLoading('#role_menu_form .lay-submit-btn', text)
	    })
	    return false;
	})
    
	// 开关
	form.on('switch(stateSwitch)', function(data) {
		var rid = $(this).attr('rid');
	    var state = data.elem.checked?"0":"1";
	    getAjax(interUrl.updateState, {rid: rid, state: state}, 'post', function (res) {
	    	showTips(res.msg);
	    })
		return false;
	})
	
})

/**
 * 获取菜单选中值-----------------------------------------------------------
 */
function getTreeValue(list, mids){
	$(list).each(function (i, o) {
		if(o.id > 0){
			mids.push(o.id);
		}
		getTreeValue(o.children, mids);
    });
}


